import 'package:get/get.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:flutter/material.dart'; // 新增这行

class SettingsController extends GetxController {
  var isDark = false.obs;
  var locale = const Locale('en', 'US').obs;

  Future<void> load() async {
    final sp = await SharedPreferences.getInstance();
    isDark.value = sp.getBool('isDark') ?? false;
    final lang = sp.getString('lang') ?? '';
    if (lang == 'zh') locale.value = const Locale('zh', 'CN');
  }

  Future<void> saveTheme(bool dark) async {
    final sp = await SharedPreferences.getInstance();
    await sp.setBool('isDark', dark);
    isDark.value = dark;
  }

  Future<void> setLocale(Locale l) async {
    final sp = await SharedPreferences.getInstance();
    await sp.setString('lang', l.languageCode);
    locale.value = l;
    Get.updateLocale(l);
  }
}
